<?php

	/**
	 *  Class to handle login and logout
	 */
	 
	class LoginHandler
	{
		//Sessionhandler to check if logged in
		private $m_sessionLocation = "LoginHandler::HasLoggedin";

		//Function to handle login.
		//returns true if logged in, else false.
		public function IsLoggedIn()
		{
			if (isset($_SESSION[$this->m_sessionLocation])) {
				return true;
			} else {
				return false;
			}
			 
		}
				
		//Function to handle login.
		//Check if logged in allready and validate inputs
		//returns true if login done, else false.
		public function DoLogin($name, $password)
		{
			if ($this->IsLoggedIn() == true) {
				return false;
			} else {
				switch ($name) {
					case 'frida':
						if ($password == "xxxxx") {
							$_SESSION[$this->m_sessionLocation] = true;
							return true;
						} else {
							return false;
						}
						break;
						
					case 'user':
						if ($password == "pw") {
							$_SESSION[$this->m_sessionLocation] = true;
							return true;
						} else {
							return false;
						}
						break;
					
					case 'admin':
						if ($password == "admin") {
							$_SESSION[$this->m_sessionLocation] = true;
							return true;
						} else {
							return false;
						}
						break;
				
					default:
						return false;
						break;
				}
			}
		}
				
		
		//Function to handle log out.
		//if logged in then logs out
		//returns true if logout done, else false.
		public function DoLogout()
		{
        	 if($this->IsLoggedIn() == true) {
             	unset($_SESSION[$this->m_sessionLocation]);
			 }
		}
		
		//Test to test functions
		public function Test()
		{
			$this->DoLogout();
			
			if ($this->IsLoggedIn() == true) {
				echo "IS NOT LOGGED IN BUT SAYS SO";
				return false;
			} 
			
			if ($this->DoLogin("fridab", "aaa") == true) {
				echo "WRONG USERNAME AND PASSWORD BUT LOGS IN aNYWAY";
				return false;
			}
			
			if ($this->DoLogin("frida", "xxxxx") == false) {
				echo "DOESNT LOG IN THOUGH CORRECT USERNAME AND PASSWORD";
				return false;
			}
			
			if ($this->IsLoggedIn() == false) {
				echo "IS LOGGED IN BUT SAYS ITS NOT";
				return false;
			} 
			
			if ($this->DoLogout()) {
				echo "CAN LOG OUT BUT SAYS IT CANT";
				return false;
			} 
			
			if ($this->DoLogin("frida", "aaa") == true) {
				echo "DOES LOG IN THOUGH WRONG PASSWORD";
				return false;
			}
		}
				
	}
